<?php
define('HTXSESSIONURL','http://192.168.1.102/htxsession/');//存储地址
define('HTXSESSIONCOOKIEDOMCOM','192.168.1.102');//session对应生效域名
/**
 * 似session类,在config.php后引用
 */
class htxsession {
    protected $_htxsessionurl;
    protected $_htxsessiondomcom;
    
    public function __construct($url='',$domcom='') {
        if ($url) {
            $this->_htxsessionurl = $url;
        } else {
            $this->_htxsessionurl = HTXSESSIONURL;
        }
        if ($url) {
            $this->_htxsessiondomcom = $domcom;
        } else {
            $this->_htxsessiondomcom = HTXSESSIONCOOKIEDOMCOM;
        }
    }
    
    /**
     * 获取数据
     * @param array $data
     * @return string
     */
    private function _senddata($data) {
        $data=http_build_query($data);
        $opts = array(
            'http'=>array(
              'method'=>"POST",
              'header'=>"Content-type:application/x-www-form-urlencoded\r\nContent-Length:".strlen($data)."\r\n",
              'content'=> $data
            )
        );
        $context = stream_context_create($opts);
        return file_get_contents($this->_htxsessionurl, FALSE, $context);
    }


    /**
     * 取得设置
     * @return array
     */
    public function getsetting(){
        $data = array('op'=>'setting');
        return $this->_senddata($data);
    }
    
    /**
     * 创建新id
     * @param string cookie名
     * @return string id
     */
    public function create($htxsessionname){
        $data = array('op'=>'create','ip'=>$_SERVER['REMOTE_ADDR']);
        $ra = $this->_senddata($data);
        setcookie($htxsessionname, $ra, 0, '/', $this->_htxsessiondomcom);
        return $ra;
    }
    
    /**
     * 读取全部内容
     * @param string cookie名
     * @return array 内容
     */
    public function read($htxsessionname) {
        if (isset($_COOKIE[$htxsessionname])) {
            $data = array('op'=>'read','ip'=>$_SERVER['REMOTE_ADDR'],'id'=>$_COOKIE[$htxsessionname]);
            $ra = $this->_senddata($data);
            return json_decode($ra, TRUE);
        }
    }
    
    /**
     * 读取部分内容
     * @param string cookie名
     * @param string 数据键名 name1,name2,name3
     * @return array
     */
    public function readpart($htxsessionname, $keys) {
        if (isset($_COOKIE[$htxsessionname])) {
            $data = array('op'=>'read','ip'=>$_SERVER['REMOTE_ADDR'],'id'=>$_COOKIE[$htxsessionname],'keys'=>$keys);
            $ra = $this->_senddata($data);
            return json_decode($ra, TRUE);
        }
    }
    
    /**
     * 更新部分写入
     * @param string cookie名
     * @param array $content
     * @return intORbool
     */
    public function write($htxsessionname, $content, $id='') {
        $id ? $id : (isset($_COOKIE[$htxsessionname]) ? $id = $_COOKIE[$htxsessionname] : $id = '');
        if ($id) {
            $data = array('op'=>'write','ip'=>$_SERVER['REMOTE_ADDR'],'id'=>$id,'content'=>json_encode($content));
            $ra = $this->_senddata($data);
            return $ra;
        }
    }
    
    /**
     * 清空后写入
     * @param string cookie名
     * @param array 内容
     * @return intORbool
     */
    public function resetwrite($htxsessionname, $content, $id='') {
        $id ? $id : (isset($_COOKIE[$htxsessionname]) ? $id = $_COOKIE[$htxsessionname] : $id = '');
        if ($id) {
            $data = array('op'=>'resetwrite','ip'=>$_SERVER['REMOTE_ADDR'],'id'=>$_COOKIE[$htxsessionname],'content'=>json_encode($content));
            $ra = $this->_senddata($data);
            return $ra;
        }
    }
    
    /**
     * 销毁
     * @param string cookie名
     * @return bool
     */
    public function destroy($htxsessionname){
        if (isset($_COOKIE[$htxsessionname])) {
            $data = array('op'=>'destroy','ip'=>$_SERVER['REMOTE_ADDR'],'id'=>$_COOKIE[$htxsessionname]);
            $ra = $this->_senddata($data);
            setcookie($htxsessionname, NULL, 0, '/', HTXSESSIONCOOKIEDOMCOM);
            return $ra;
        }
    }
}
